package com.dpj.CookieServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
//22
@WebServlet("/aServlet")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //cookie不能传递中文，所以想要传递中文就要使用url编码
        String value="饼干";
        //url编码
        value= URLEncoder.encode(value,"utf-8");
        Cookie cookie=new Cookie("food",value);

        //设置最大存活时间 一周，一般情况下，cookie存在游览器的内存，我们关闭网页后就会清楚
        //但是当我们设置了最大时间后，它就存在了我们的游览器所在的硬盘中了
        cookie.setMaxAge(60*60*24*7);
        resp.addCookie(cookie);
    }
}
